<?php

namespace service\excel;
/**
 *  excel 服务类
 * Class JsonReturnService
 * @package Service
 */
Class ExcelWrite
{

    public static function excelExport($expTitle, $initHeader, $tableData,$ajax = false)
    {
        set_time_limit(0);
        vendor('PHPExcel.PHPExcel');
        $objPHPExcel = new \PHPExcel();
        $cellName = array(
            'A',
            'B',
            'C',
            'D',
            'E',
            'F',
            'G',
            'H',
            'I',
            'J',
            'K',
            'L',
            'M',
            'N',
            'O',
            'P',
            'Q',
            'R',
            'S',
            'T',
            'U',
            'V',
            'W',
            'X',
            'Y',
            'Z',
            'AA',
            'AB',
            'AC',
            'AD',
            'AE',
            'AF',
            'AG',
            'AH',
            'AI',
            'AJ',
            'AK',
            'AL',
            'AM',
            'AN',
            'AO',
            'AP',
            'AQ',
            'AR',
            'AS',
            'AT',
            'AU',
            'AV',
            'AW',
            'AX',
            'AY',
            'AZ'
        );


        $objPHPExcel->setActiveSheetIndex(0);
        $i = 0;
        $lengthArr = [];
        foreach ($initHeader as $k => $item) {
            $lengthArr[$cellName[$i]] = strlen($item);
            $objPHPExcel->setActiveSheetIndex(0)->setCellValueExplicit($cellName[$i] . '1', $item, \PHPExcel_Cell_DataType::TYPE_STRING);
            $objPHPExcel->getActiveSheet()->getStyle($cellName[$i])->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
            $objPHPExcel->getActiveSheet()->getColumnDimension($cellName[$i])->setAutoSize(true);
            $i++;
        }
        $num = 0;
        $key = 1;

        foreach ($tableData as $k => $item) {
            //设置循环从第二行开始
            $cell = 0;
            $key++;
            $num++;
            foreach ($initHeader as $index => $value) {
                $objPHPExcel->getActiveSheet()->setCellValueExplicit($cellName[$cell] . $key, $item[$index],\PHPExcel_Cell_DataType::TYPE_STRING);
                $strLength = strlen($item[$index]);
                if ($strLength > $lengthArr[$cellName[$cell]]) {
                    $lengthArr[$cellName[$cell]] = $strLength * 1.5;
                }
                $objPHPExcel->getActiveSheet()->getColumnDimension($cellName[$cell])->setWidth($lengthArr[$cellName[$cell]]);
                $cell++;
            }
            unset($tableData[$k]);
        }

        $fileName = $expTitle . date('Ymd').'.xls';
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename='.$fileName);
        header('Cache-Control: max-age=0');
        header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
        header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header('Pragma: public'); // HTTP/1.0
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        if(!$ajax){
            exit;
        }
        $xlsData = ob_get_contents();
        ob_end_clean();
        return ret_template(true,'',['filename'=>$fileName,'file' => "data:application/vnd.ms-excel;base64," . base64_encode($xlsData)]);
    }


    public static function csvExport($titleName, $headList,$data)
    {
        $fileName = $titleName . date('Y_m_d');

        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
        header('Cache-Control: max-age=0');
        $fp = fopen('php://output', 'a');
        fputcsv($fp, $titleName);
        $cellTitle = [];
        foreach ($headList as $key => $value) {
            $cellTitle[$key] = iconv('utf-8', 'gbk', $value);
        }
        fputcsv($fp, $cellTitle);
        //计数器
        $num = 0;
        $limit = 100;
        foreach ($data as $k => $item) {
            $num++;
            //刷新一下输出buffer，防止由于数据过多造成问题
            if ($limit == $num) {
                ob_flush();
                flush();
                $num = 0;
            }
            $row = [];
            foreach ($headList as $key => $index) {
                $row[] = iconv('utf-8', 'gbk', $item[$key]);
            }
            fputcsv($fp, $row);
            unset($row,$data[$k]);
        }
        ob_flush();
        flush();
        exit;
    }
}

